<?php

/**
 * Picture actions.
 *
 * @package    wills
 * @subpackage Picture
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class PictureActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	 $this->areas = Doctrine::getTable('Area')->getHomepageList();     //获得当前赛区
	$aid=sfContext::getInstance()->getUser()->getAttribute('id');//登录会员id
	$this->contestant=$contestant =Doctrine_Query::create()
					 ->from('Contestant')
					 ->Where('application_id = ?', $aid)
					 ->fetchOne();
	if($contestant){
	$cid=$contestant->getId();//登录会员id转换为选手id，以获取图片用到的contestant_id
	$this->cid=$cid;
	/*$this->pictures = Doctrine::getTable('Picture')
      ->createQuery('a')
	  ->addWhere('contestant_id = ?', $cid)
      ->execute();*/

	$this->pager = new sfDoctrinePager(
      'Picture',
      sfConfig::get('app_per_page_usercenterphoto')
    );
    $this->pager->setQuery(Doctrine::getTable('Picture')->createQuery('a')->addWhere('contestant_id = ?', $cid));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
	$this->pictures = $this->pager->getResults();

	//如果用户在网址中输入一个不存在的页码，给予提示超出页码，而不至于错误提示还未上传照片
	if ($request->getParameter('page')>$this->pager->getLastPage()){
		 $this->pageoverflow=1;
		}
	}
  }

  public function executeShow(sfWebRequest $request)
  {
	 $this->areas = Doctrine::getTable('Area')->getHomepageList(); //获得当前赛区
	$aid=sfContext::getInstance()->getUser()->getAttribute('id');//登录会员id
	$contestant =Doctrine_Query::create()
					 ->from('Contestant')
					 ->Where('application_id = ?', $aid)
					 ->fetchOne();
	$cid=$contestant->getId();//登录会员id转换为选手id，以获取图片用到的contestant_id
	$this->cid=$cid;
    $this->picture = Doctrine::getTable('Picture')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->picture);
  }

    public function executeUpload(sfWebRequest $request)
  {
	 $this->areas = Doctrine::getTable('Area')->getHomepageList(); //获得当前赛区
    $aid=sfContext::getInstance()->getUser()->getAttribute('id');//登录会员id
	$this->contestant=$contestant =Doctrine_Query::create()
				 ->from('Contestant')
				 ->Where('application_id = ?', $aid)
				 ->fetchOne();
	if($contestant){
	$cid=$contestant->getId();//登录会员id转换为选手id，以获取图片用到的contestant_id
	$this->cid=$cid;//模板用到的选手cid-cid

	$this->form = new PictureForm();
	}
    //$this->forward404Unless($this->picture);
  }


  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PictureForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
	 $this->areas = Doctrine::getTable('Area')->getHomepageList(); //获得当前赛区
	$aid=sfContext::getInstance()->getUser()->getAttribute('id');//登录会员id
	$contestant =Doctrine_Query::create()
				 ->from('Contestant')
				 ->Where('application_id = ?', $aid)
				 ->fetchOne();
	$cid=$contestant->getId();//登录会员id转换为选手id，以获取图片用到的contestant_id
	$this->cid=$cid;//模板用到的选手cid-cid

    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PictureForm();

    $this->processForm($request, $this->form);
    $this->setTemplate('upload');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($picture = Doctrine::getTable('Picture')->find(array($request->getParameter('id'))), sprintf('Object picture does not exist (%s).', $request->getParameter('id')));
    $this->form = new PictureForm($picture);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($picture = Doctrine::getTable('Picture')->find(array($request->getParameter('id'))), sprintf('Object picture does not exist (%s).', $request->getParameter('id')));
    $this->form = new PictureForm($picture);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($picture = Doctrine::getTable('Picture')->find(array($request->getParameter('id'))), sprintf('Object picture does not exist (%s).', $request->getParameter('id')));
    $picture->delete();
	$this->getUser()->setFlash('message', '恭喜您，删除成功！');


    $this->redirect('@picture');
  }


  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	
    if ($form->isValid())
    {
      $picture = $form->save();
	  $this->resizeImages($picture);//生成缩略图
	  $this->getUser()->setFlash('message', '恭喜您，上传成功！');

	  $this->redirect('@picture');
    }
  }	

  
  protected function resizeImages($object)
	 {
		$upload_dir = 'uploads/contestant/picture/';
		$thumbs_dir = 'uploads/contestant/picture/thumb/';
		$ptoto = $object->getPicture();
		if ($ptoto && file_exists($upload_dir.$ptoto))
		{
			$thumbnail = new sfThumbnail(135, 90);
			$thumbnail->loadFile($upload_dir.$ptoto);
			$thumbnail->save($thumbs_dir.'thumb_'.$ptoto);

			$object->setThumb('thumb_'.$ptoto)->save();//更新thumb字段
		} 


	  }



}
